package com.ruoyi.kjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.kjy.domain.Enterprise;
import com.ruoyi.kjy.domain.StorageApplication;
import com.ruoyi.kjy.service.IEnterpriseService;
import com.ruoyi.kjy.service.IStorageApplicationService;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/storageApplication")
public class StorageApplicationController extends BaseController {
    @Autowired
    private IStorageApplicationService storageApplicationService;
    @Autowired
    private IEnterpriseService enterpriseService;

    /**
     * 查询入库申请列表
     */
    @GetMapping("/list")
    public AjaxResult list(Integer current, Integer size, StorageApplication storageApplication) {
        if (Objects.isNull(current)) {
            current = 1;
        }
        if (Objects.isNull(size)) {
            size = 10;
        }
        if (size >= 20) {
            size = 20;
        }
        Page<StorageApplication> list = storageApplicationService.page(new Page<>(current, size), new LambdaQueryWrapper<StorageApplication>().setEntity(storageApplication));
        return AjaxResult.success(list);
    }

    /**
     * 获取入库申请详情
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        StorageApplication storageApplication = storageApplicationService.getById(id);
        if (storageApplication != null) {
            return AjaxResult.success(storageApplication);
        } else {
            return AjaxResult.error("未找到该入库申请");
        }
    }

    /**
     * 新增入库申请
     */
    @Log(title = "申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StorageApplication storageApplication) {

        storageApplication.setFunding(new BigDecimal(0));
        Long enterpriseId = SecurityUtils.getLoginUser().getSysUser().getEnterpriseId();
        Enterprise enterprise =new Enterprise();
        enterprise.setHightechEnterprises("0");
        if(enterpriseId!=null && enterpriseId != 0 ){
            enterprise = enterpriseService.getById(enterpriseId);
            if (enterprise != null && enterprise.getHightechEnterprises().equals("1")) {
                throw new ServiceException("企业已入库");
            }
        }

        // 判断该用户是否有入库申请
        StorageApplication storageApplication1 = new StorageApplication();
        storageApplication1.setApplicantId(SecurityUtils.getUserId());
        List<StorageApplication> list = storageApplicationService.list(new LambdaQueryWrapper<StorageApplication>().setEntity(storageApplication));
        if (list != null && !list.isEmpty()) {
            throw new ServiceException("您已提交申请");
        }
        storageApplication.setState(0);
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        storageApplication.setEnterpriseId(sysUser.getEnterpriseId());
        storageApplication.setApplicantId(sysUser.getUserId());
        return toAjax(storageApplicationService.save(storageApplication));
    }

    /**
     * 管理端新增入库申请
     */
    @Log(title = "管理端申请", businessType = BusinessType.INSERT)
    @PostMapping
    @RequestMapping("/adminAdd")
    public AjaxResult adminAdd(@RequestBody StorageApplication storageApplication) {
        storageApplication.setFunding(new BigDecimal(0));
        Long enterpriseId = storageApplication.getEnterpriseId();
        if(enterpriseId!=null&& enterpriseId != 0){
            Enterprise enterprise = enterpriseService.getById(enterpriseId);
            if (enterprise != null && enterprise.getHightechEnterprises().equals("1")) {
                //throw new ServiceException("该企业已入库");
                return AjaxResult.warn("该企业已入库");
            }
        }
        storageApplication.setState(0);
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        storageApplication.setApplicantId(sysUser.getUserId());
        return toAjax(storageApplicationService.save(storageApplication));
    }


    /**
     * 修改申请
     */
    @Log(title = "申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StorageApplication storageApplication) {
        Integer state = storageApplication.getState();
        StorageApplication byId = storageApplicationService.getById(storageApplication.getId());
        if (state == 1) {
            String username = SecurityUtils.getUsername();
            String createBy = storageApplication.getCreateBy();
            Enterprise enterprise = enterpriseService.getById(byId.getEnterpriseId());
//            if (username.equals(createBy)) {
//                storageApplication.setState(0);
//                enterprise.setHightechEnterprises("0");
//            } else {
//                enterprise.setHightechEnterprises("1");
//            }
            enterprise.setHightechEnterprises("1");
            enterpriseService.updateById(enterprise);
        }
        return toAjax(storageApplicationService.updateById(storageApplication));
    }

    /**
     * 删除申请
     */
    @Log(title = "申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(storageApplicationService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 查询企业列表
     */
    @GetMapping("/listEnterprise")
    public AjaxResult listEnterprise(Enterprise enterprise)
    {
        LambdaQueryWrapper<Enterprise> queryWrapper = new LambdaQueryWrapper<Enterprise>().setEntity(enterprise);
        queryWrapper.or().like(enterprise.getName() != null, Enterprise::getName, enterprise.getName());
        List<Enterprise> list = enterpriseService.list(queryWrapper);
        return AjaxResult.success(list);
    }
}
